package org.kernely.security.ui.group.mgmt;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.kernely.security.dao.UserDAO;
import org.kernely.security.dao.UserDetailsDAO;
import org.kernely.security.persistence.Group;
import org.kernely.security.persistence.User;
import org.kernely.security.persistence.UserDetails;
import org.kernely.ui.mvp.KernelyEventSender;
import org.kernely.ui.mvp.KernelyPresenter;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;

public class AddEditGroupManagementPresenter extends KernelyPresenter<AddEditGroupManagementView> implements KernelyEventSender{

	/**
	 * Group to edit in edition mode
	 */
	private Group group;
	private boolean editMode = false;
	/**
	 * Available users for this group
	 */
	private Set<User> selectableUsers;
	/**
	 * Users contained in this group
	 */
	private Set<User> groupUsers;

	/**
	 * Default Constructor
	 */
	public AddEditGroupManagementPresenter(){}

	/**
	 * Constructor used in edit mode, with the group to edit
	 * @param group : the group to edit
	 */
	public AddEditGroupManagementPresenter(Group group){
		this.group = group;
		this.editMode = true;
	}
	
	/**
	 * @see org.kernely.ui.mvp.IKernelyPresenter#bind()
	 */
	@Override
	public final void bind() {
		UserDAO udao = UserDAO.getInstance();

		groupUsers = new HashSet<User>();
		selectableUsers = udao.getUsers();

		// In edition mode, fill the fields with the informations
		if(group != null && editMode){
			this.view.getName().setValue(group.getName());
			groupUsers = group.getUsers();
			for(User u : groupUsers){
				// We remove the user in selectableUsers list
				selectableUsers.remove(u);
			}
		}

		lists();


		// Add a listener to the add button
		this.view.getAddButton().addListener(new Button.ClickListener() {
			/**
			 * The Serial Version ID
			 */
			private static final long serialVersionUID = -115612811698526312L;

			/**
			 * Action when the button is clicked
			 */
			public void buttonClick(ClickEvent event) {
				addUser();
			}
		});
		
		// Add a listener to the delete button
		this.view.getDeleteButton().addListener(new Button.ClickListener() {
			/**
			 * Action when the button is clicked
			 */
			public void buttonClick(ClickEvent event) {
				removeUser();
			}
		});
	}

	/**
	 * Adds a new user in the group's users list
	 */
	protected final void addUser() {
		UserDAO udao = new UserDAO();
		int userID = (Integer) this.view.getUsersBox().getValue();
		selectableUsers.remove(udao.getUser(userID));
		groupUsers.add(udao.getUser(userID));
		//Actualize lists
		lists();
	}
	
	/**
	 * Remove a user from the group's users list
	 */
	protected final void removeUser() {
		UserDAO udao = new UserDAO();
		Collection<Integer> removedUserID = (Collection<Integer>)this.view.getUsersList().getValue();
		for (Integer userID : removedUserID){
			selectableUsers.add(udao.getUser(userID));
			groupUsers.remove(udao.getUser(userID));			
		}
		//Actualize lists
		lists();
	}

	/**
	 * @return the groupUsers
	 */
	public final Set<User> getGroupUsers() {
		return groupUsers;
	}

	/**
	 * Lists the selectable users in the combo box and the group users in the list.
	 */
	private void lists(){
		UserDetailsDAO uddao = new UserDetailsDAO();
		
		// Remove all items
		this.view.getUsersBox().removeAllItems();
		this.view.getUsersList().removeAllItems();

		//List the group users
		for(User u : groupUsers){
			if (u.getEnabled() == 1){
				UserDetails ud = uddao.getUserDetails(u);
				// Display user in the group's users list
				this.view.getUsersList().addItem(u.getId());
				this.view.getUsersList().setItemCaption(u.getId(),
						ud.getFirstname()
						+ " "
						+ ud.getName()
						+ " (" + ud.getMail() + ")");
			}
		}

		// List selectable users
		for(User u : selectableUsers){
			UserDetails ud = uddao.getUserDetails(u);
			// Only enabled users can be added to a group
			if (ud.getUser().getEnabled() == 1){
				this.view.getUsersBox().addItem(u.getId());
				this.view.getUsersBox().setItemCaption(u.getId(),
						ud.getFirstname()
						+ " "
						+ ud.getName()
						+ " (" + ud.getMail() + ")");
			}
		}
	}
	
}
